package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentExample;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.IStudentService;
import com.hzit.util.MyBatisUtil;
import com.hzit.vo.StudentVO;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class StudentServiceImpl implements IStudentService {

	//1. 条件查询带分页
	@Override
	public PageResult<StudentDTO> search(int page, int pageSize, StudentVO vo) throws IOException {
		//1.0 开始记录分页
		PageHelper.startPage(page,pageSize);
		//1.1 得到SqlSessionFactory对象
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		//1.2 开始进行查询
		try(SqlSession sqlSession = sqlSessionFactory.openSession(true)){
			//1.2.1 得到mapper对象
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
			//1.2.2 开始条件查询
			StudentExample example = new StudentExample();
			//1.2.3 将整个查询看作是一个对象,而这个查询对象中的条件对象就是下面的这个Criteria对象
			StudentExample.Criteria criteria = example.createCriteria();
			if(!Objects.isNull(vo)){
				// ① 关于姓名的条件查询
				if(StrUtil.isNotBlank(vo.getSname())){
					criteria.andSnameLike("%" + vo.getSname() + "%");
				}
				// ② 关于地址的条件查询
				if(StrUtil.isNotBlank(vo.getAddr())){
					criteria.andAddrLike("%" + vo.getAddr() + "%");
				}
				// ③ 关于所在班级的查询
				if(vo.getCid() != null && vo.getCid() != 0){
					criteria.andCidEqualTo(vo.getCid());
				}
			}
			//1.3 根据条件查询得到查询结果
			List<Student> students = mapper.selectByExample(example);
			//1.4 转换为Page对象
			Page<Student> studentPage = (Page<Student>) students;
			//1.5 得到当前页的记录集合
			List<Student> result = studentPage.getResult();
			//1.6 将上面的List<Student>转换为List<StudentDTO>集合
			List<StudentDTO> collect = result.stream().map(m -> {
				StudentDTO dto = new StudentDTO();
				BeanUtil.copyProperties(m, dto, true);
				//1.6.1 根据班级号查询班级名字
				Classes classes = classesMapper.selectByPrimaryKey(m.getCid());
				//1.6.2 为dto指定班级名字
				dto.setCname(classes.getCname());
				return dto;
			}).collect(Collectors.toList());
			//1.7 最后返回
			return new PageResult<>(collect,studentPage.getTotal());
		}
	}
	// 添加学生
	@Override
	public void insert(StudentDTO dto) throws IOException {
		//1. 得到SqlSessionFactory对象
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		//2. 开始进行查询
		try(SqlSession sqlSession = sqlSessionFactory.openSession(true)){
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			//2.1 将DTO转换student对象
			Student student = new Student();
			BeanUtil.copyProperties(dto,student,true);
			mapper.insert(student);
		}
	}
	// 修改学生
	@Override
	public void update(StudentDTO dto) throws IOException {
		//1. 得到SqlSessionFactory对象
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		//2. 开始进行查询
		try(SqlSession sqlSession = sqlSessionFactory.openSession(true)){
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			//2.1 将DTO转换student对象
			Student student = new Student();
			BeanUtil.copyProperties(dto,student,true);
			mapper.updateByPrimaryKeySelective(student);
		}
	}

	//3. 删除学生
	@Override
	public void delete(String sids) throws IOException {
		//1. 得到SqlSessionFactory对象
		SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
		//2. 开始进行查询
		try(SqlSession sqlSession = sqlSessionFactory.openSession(true)){
			StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
			String[] ids = sids.split(",");
			//删除方法一：
			// for (int i = 0; i < ids.length; i++) {
			// 	mapper.deleteByPrimaryKey(Integer.parseInt(ids[i]));
			// }
			//删除方法二：
			StudentExample example = new StudentExample();
			StudentExample.Criteria criteria = example.createCriteria();
			List<Integer> sidsList = Arrays.stream(ids).map(m ->new Integer(m)).collect(Collectors.toList());
			criteria.andSidIn(sidsList);
			mapper.deleteByExample(example);
		}
	}
}
